package com.qc.ayouth.shop.admin.service.system.impl;

import com.qc.ayouth.shop.admin.beans.dto.CategoryDto;
import com.qc.ayouth.shop.admin.beans.dto.CategorySimpleDto;
import com.qc.ayouth.shop.admin.service.system.AdminStorageService;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.core.utils.BeanCopyUtil;
import com.qc.ayouth.shop.dbs.entity.TShopCategory;
import com.qc.ayouth.shop.dbs.service.shop.TShopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Haojg on 2020/7/9
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService {
    @Autowired
    TShopCategoryService tShopCategoryService;

    @Override
    public ServiceResultBO getGoodsCategoryList() {
        ServiceResultBO bo = new ServiceResultBO();
        // 1.获取一级列表
        List<TShopCategory> catList = tShopCategoryService.getCategoryMain();
        List<CategoryDto> categoryDtoList = new ArrayList<>();
        for (TShopCategory cate : catList) {
            CategoryDto categoryDto = BeanCopyUtil.copy(cate, CategoryDto.class);

            // 2.获取分类下的二级分类
            List<TShopCategory> subCatList = tShopCategoryService.getCategoryChildren(cate.getId());
            List<CategoryDto> subCatDtoList = BeanCopyUtil.copyList(subCatList, CategoryDto.class);
            categoryDto.setChildren(subCatDtoList);

            // 3.返回分类
            categoryDtoList.add(categoryDto);
        }

        // 返回组装后的结果
        bo.add("categoryList", categoryDtoList);
        bo.success();
        return bo;
    }

}
